package com.tmsb.utils;

import redis.clients.jedis.JedisCluster;

import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *Description:
 *Version:1.0.0
 *@author shihb
 *@date 2020/6/11 17:40
 */
public class Util {

  /**
   * 获取传入时间当天凌晨的毫秒数
   * @return 今天凌晨的毫秒数
   */
  public static Long getZeroPointTimestamps(Long nowMillis){
    if(nowMillis==0){
      nowMillis=System.currentTimeMillis();
    }
    return nowMillis-(nowMillis+60*60*8*1000)%Const.DAY_MILLIS;
  }
  /**
   * 获取当前时间当天凌晨的毫秒数
   * @return 今天凌晨的毫秒数
   */
  public static Long getZeroPointTimestamps(){
    return getZeroPointTimestamps(0L);
  }
  /**
   * 获取当前时间当天凌晨的秒数
   * @return 今天凌晨的秒数
   */
  public static Long getZeroPointTimestampsSecond(){
    return getZeroPointTimestamps(0L)/1000;
  }
  /**
   * 获取传入时间当天凌晨的数
   * @return 今天凌晨的秒数
   */
  public static Long getZeroPointTimestampsSecond(Long second){
    return getZeroPointTimestamps(second*1000)/1000;
  }


  /**
   * 获取明天凌晨的数
   * @return 明天凌晨的毫秒数
   */
  public static Long getTomorrowZeroPointTimestamps(){
    long nowMillis=System.currentTimeMillis();
    return nowMillis-(nowMillis+60*60*8*1000)%Const.DAY_MILLIS+Const.DAY_MILLIS;
  }

  /**
   * 维护一个全局变量，用来统计ES Alarm的id数
   */
  static JedisCluster jedisCluster;
  static Long id= 0L ;
  static Lock lock = new ReentrantLock();
  public static Long getAlarmId(){
    lock.lock();
    try {
      jedisCluster = RedisCluster.getInstance(ConfUtil.getRedisCluster()).getRedisClusterList().get(0);
      id = Long.parseLong(jedisCluster.get("alarmId"));
      jedisCluster.set("alarmId",++id+"");
      return id;
    }finally {
      lock.unlock();
    }
  }


}
